package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import cc.plumgo.j2me.utils.CustomPage;
import cc.plumgo.ss.core.Console;
import cc.plumgo.ss.core.Constants;
import cc.plumgo.ss.core.SchoolMidlet;

public class OverPage extends CustomPage {

	public static final int OVER_HIDE = 0;
	public static final int OVER_SHOW = 1;
	public static final int OVER_STATE_NUM = 2;
	
	public static final int NEW = 0;
	public static final int EXIT = 1;
	public static final int SELECTED_NUM = 2;
	
	private Image backImage;
	private Image textImage;
	private int selected = NEW;
	
	public OverPage() {
		super();
	}
	
	public void process(int keyStates, Graphics g) {
		switch(state) {
		case OVER_HIDE : {
			break;
		}
		case OVER_SHOW : {
			loadImage();
			g.drawImage(backImage, 0, 0, 0);
			int x = 5;
			int color = 0xFFFFFFFF;
			g.setColor(color);
			switch(selected) {
			case EXIT : 
				x = 125;
			case NEW :
			}
			g.fillRect(x, 175, 110, 35);
			g.drawImage(textImage, 10, 180, 0);
			
			if ((keyStates & GameCanvas.FIRE_PRESSED) != 0) {
				switch (selected) {
				case NEW : {
					Console.getInstance().getGameListener().restart();
					break;
				}
				case EXIT :
					SchoolMidlet.midlet.notifyDestroyed();
				}
			}
			if ((keyStates & GameCanvas.UP_PRESSED) != 0) {
				changeSelected();
			}
			if ((keyStates & GameCanvas.DOWN_PRESSED) != 0) {
				changeSelected();
			}
			break;
		}
		}
	}
	
	private void changeSelected() {
		selected = (selected == NEW) ? EXIT : NEW; 
	}
	
	public void loadImage() {
		if (backImage == null) {
			try {
				backImage = Image.createImage(Constants.UI_IMG_OVER_BACK);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (textImage == null) {
			try {
				textImage = Image.createImage(Constants.UI_IMG_OVER_TEXT);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
